package com.cyy.dao;

import com.cyy.util.dbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class loginRecordDao extends userDao{
    public void addRecord(String name,String dateTime,int identity) throws SQLException, ClassNotFoundException {
//        开始添加登录记录
        Connection conn = dbUtil.getConnection();
        String sql="insert into loginrecord values(?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,name);
        pstmt.setString(2,dateTime);
        pstmt.setInt(3,identity);
        int i = pstmt.executeUpdate();
    }

    public ArrayList<String> selectAllByName(String userName,int identity) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql="select *from loginrecord where name=? and identity=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,userName);
        pstmt.setInt(2,identity);
        ResultSet resultSet = pstmt.executeQuery();
        ArrayList<String> loginTimes=null;
        String loginTime;
        while(resultSet.next())
        {
            if(loginTimes==null)
            {
                loginTimes=new ArrayList<>();
            }
            loginTime = resultSet.getString("loginTime");
            loginTimes.add(loginTime);
        }
        return loginTimes;
    }
    public void deleteAll(String userName,int identity) throws SQLException, ClassNotFoundException {
        Connection conn = dbUtil.getConnection();
        String sql = "delete from loginrecord where name=? and identity=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, userName);
        pstmt.setInt(2, identity);
        int resultSet = pstmt.executeUpdate();
        dbUtil.close(null, pstmt, conn);
    }
}
